#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#include <utils.h>
#include "iot_flash.h"
#include "iot_uart.h"
#include <stdarg.h>

void mm_strcat(char * t,char * s){
    while(*t!=0) t++;
    while((*t++=*s++)!=0);
}

void mm_split(char *src,const char *separator,char **dest,int *num) {
    char *pNext;
    int count = 0;
    if (src == NULL || strlen(src) == 0)
        return;
    if (separator == NULL || strlen(separator) == 0)
        return;   
	char *strtok(char *str, const char *delim); 
    pNext = strtok(src,separator);
    while(pNext != NULL) {
        *dest++ = pNext; 
        ++count; 
        pNext = strtok(NULL,separator);  
    }  
    *num = count;
}

int mm_find_hex(char * src,char hex,int start,int length){
    int count = start;
    for(count = start;count<length;count++){
        if(src[count] == hex){
            return count;
        }
    }
    return -1;
}

void strccat1(char *str1,int str1Len,char *str2,int str2Len){
    
}

// void mm_strdel(char str[],char c){
// 	int j=0;
// 	for(int i=0;str[i]!='\0';i++)
// 		if(str[i] != c)
// 			str[j++]=str[i];
// 	str[j]='\0';
// }



void mm_print_hex(uint8_t str[],int size){
    for(int i=0;i<size;i++){
        printf("%02X ", str[i]);
    }
}

void mm_print(uint8_t str[],int size){
    for(int i=0;i<size;i++){
        printf("%c", str[i]);
    }
}

void mm_flash_read(uint32_t addr,uint8_t *buf,uint16_t len){
    IoTFlashRead(addr-0x400000,len,(uint8_t*)buf);
}
void mm_flash_write(uint32_t addr,uint8_t *buf,uint16_t len){
    IoTFlashWrite(addr-0x400000,len,(uint8_t*)buf,1);
}

void mm_LogPrint(const char *format, ...){
    va_list args;
	char Data_Buff[100];
    uint32_t length;
    va_start(args, format);
    length = vsnprintf((char *)Data_Buff, 100, (char *)format, args);
    va_end(args);

    IoTUartWrite(1, (uint8_t *)Data_Buff, length);
}
